package com.njg.juc.locks;

import java.util.concurrent.TimeUnit;

/**
 * @author njg
 * @version 1.0
 * @date 2025/4/26 20:07
 */
public class DeadLockDemo2 {
    static Object lock1 = new Object();
    static Object lock2 = new Object();
    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock1){
                System.out.println("获取锁1,尝试获取锁2");
                try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (lock2){
                    System.out.println("获取锁2成功");
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (lock2){
                System.out.println("获取锁2,尝试获取锁1");
                try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
                synchronized (lock1){
                    System.out.println("获取锁1成功");
                }
            }
        }, "B").start();
    }
}
